import fileinput
import time


class OneMarkov(object):
    def __init__(self):
        self.dic1={}

    def try12(self):
        dic2={}
        n=0
        for line in fileinput.input(['D:\\train_d.csv']):
                try:
                        self.train(line,n,self.dic1)
                except:
                    continue
    def test12(self):
        count=0
        average=0
        n=0
        for line in fileinput.input(['D:\\test_d.csv']):
                try:
                    average,count=self.test(line,n,average,count,self.dic1)
                except:
                    continue
        
                    
    def train(self,line, n,dic1): # this is a training method 
        count1=0
        print "INSIDE TRAIN"
        line2= line.strip().rstrip(',')
        line1=line2.split(',') # to get the elements/domain number in a sessions
        length=len(line1)-1
        for i in range(0,length):
            j=i+1
            if dic1.has_key(line1[i]):
                value=dic1.get(line1[i])
                count1=value[0]
                dic2=value[1]
                count1=int(count1)+1
                if dic2.has_key(line1[j]):
                    dic2[line1[j]]=int(dic2.get(line1[j]))+1
                else:
                    dic2[line1[j]]=1
                    dic1[line1[i]]=(count1,dic2)
            else:
                count2=1
                dic3={}
                dic3[line1[j]]=1
                dic1[line1[i]]=(count2,dic3)
    
        
    def test(self,line4,n,average,count,dic1):
    #    for line4 in fileinput.input(['D:\\test.txt']):
        #            print line
        print "INSIDE TEST"
        list2=[]
        line5= line4.strip().rstrip(',')
        line6=line5.split(',')
        length=len(line6)-1
        for i in range(0,length):
            list2=self.recommend(line6[i],dic1)
            len1=len(list2)
    #        print "LENGTH LOSTR @",len1
            if not len1==0:
                count+=1
                j=i+1
                re=self.evaluate(list2,line6[j],count,average)   
                average=re[0] 
        self.train(line4,n,dic1)       
        print "Average:", average
        print "count", count
        return average,count
    
    
    def recommend(self,d1,dic1):
        list1 =[]
        value=dic1.get(d1)
        if (value==None):
            return list1
        print "*********"
        hash= value[1]
        list1=sorted(hash.iteritems(), key=lambda (k,v): (v,k))
        list1.reverse()
        len1=len(list1)
        resultlist=[]
        for u in range(0,len1):
            resultlist.append((list1[u])[0])
        return resultlist[0:20]
    
    
    def evaluate(self,recommendationlist,actual,count,average):
        correct=0
        print "aactural", actual
        print "rec", recommendationlist
        if actual in recommendationlist:
            average = self.calculateAccuracy(1,average,count)
            return (average,count,correct)
        else:
            average=self.calculateAccuracy(0,average,count)
            return (average,count,correct)
        
    def calculateAccuracy(self,correct,average,count):

            return ((int(count)-1)*float(average)+int(correct))/int(count)

class Tester(object):
        a= OneMarkov()
        a.__init__()
        a.try12()
        a.test12()
